import axios from 'axios'
import { getToken } from '~/composables/auth'
import { toast } from '~/composables/utils'
import store from './store';
const service = axios.create({
    baseURL: 'http://localhost:8102/api',
})// 添加请求拦截器
service.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    const token = getToken()
    if(token){
        config.headers["Authorization"] = token
    }
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
service.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    console.log("相应信息")
    console.log(response)
    if(response.data.code === 40100){
      store.dispatch('logout').finally(()=>location.reload())
    }
    if(response.data.code !== 0){
      toast(response.data.message, 'error')
      return Promise.reject(response.data.message)
    }
    return response.data.data;
  }, function (error) {
    const msg = error.response.data.msg || '请求失败'
    if(msg == "未登录"){
      store.dispatch('logout').finally(()=>location.reload())
    }
        toast(msg, 'error')
        return Promise.reject(error);
  }); 
export default service